package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.service.FuiouNotifyService;
import com.ruoyi.business.service.IWsdLeaveRecordsService;
import com.ruoyi.business.service.IWsdLeavesService;
import com.ruoyi.business.service.IWsdMealRefundsService;
import com.ruoyi.common.core.domain.dto.PayoutsDTO;
import com.ruoyi.common.core.domain.dto.RefundDTO;
import com.ruoyi.common.core.domain.dto.WsdMealRefundsDTO;
import com.ruoyi.common.core.domain.entity.WsdLeaveRecords;
import com.ruoyi.common.core.domain.entity.WsdLeaves;
import com.ruoyi.common.core.domain.entity.WsdMealRefunds;
import com.ruoyi.common.core.domain.entity.WsdPaymentRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 请假审批记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@Service
public class WsdLeaveRecordsServiceImpl  extends ServiceImpl<WsdLeaveRecordsMapper, WsdLeaveRecords> implements IWsdLeaveRecordsService
{
    @Autowired
    private WsdLeaveRecordsMapper wsdLeaveRecordsMapper;
    @Autowired
    private WsdLeavesMapper wsdLeavesMapper;
    @Autowired
    private WsdMealRefundsMapper wsdMealRefundsMapper;

    @Autowired
    private WsdPaymentRecordsMapper wsdPaymentRecordsMapper;
    @Autowired
    private FuiouNotifyService fuiouNotifyService;
    @Autowired
    private IWsdLeavesService wsdLeavesService;
    /**
     * 查询请假审批记录
     *
     * @param id 请假审批记录主键
     * @return 请假审批记录
     */
    @Override
    public WsdLeaveRecords selectWsdLeaveRecordsById(Long id) {
        return wsdLeaveRecordsMapper.selectWsdLeaveRecordsById(id);
    }

    /**
     * 查询请假审批记录列表
     *
     * @param wsdLeaveRecords 请假审批记录
     * @return 请假审批记录
     */
    @Override
    public List<WsdLeaveRecords> selectWsdLeaveRecordsList(WsdLeaveRecords wsdLeaveRecords) {
        List<WsdLeaveRecords> wsdLeaveRecordsList = wsdLeaveRecordsMapper.selectWsdLeaveRecordsLists(wsdLeaveRecords);
        if (wsdLeaveRecordsList.size()>0){
            for (WsdLeaveRecords wsdLeaveRecord : wsdLeaveRecordsList){
                List<WsdMealRefundsDTO> list = wsdMealRefundsService.selectWsdMealRefundsListById(wsdLeaveRecord.getLeaveId());
                Integer lunch = 0;
                Integer dinner = 0;
                Integer breakfast = 0;
                if (list.size()>0){
                    for (WsdMealRefundsDTO wsdMealRefund : list){
                        wsdLeaveRecord.setRefundAmount( wsdLeaveRecord.getRefundAmount().add(wsdMealRefund.getRefundAmount() ) );
                        if (wsdMealRefund.getBreakfast()==1){
                            breakfast = breakfast + 1;
                        }
                        if (wsdMealRefund.getLunch()==1){
                            lunch = lunch + 1;
                        }
                        if (wsdMealRefund.getDinner()==1){
                            dinner = dinner + 1;
                        }
                    }
                }
                wsdLeaveRecord.setBreakfast( breakfast );
                wsdLeaveRecord.setLunch( lunch );
                wsdLeaveRecord.setDinner( dinner );
            }
        }
        return wsdLeaveRecordsList;
    }

    /**
     * 新增请假审批记录
     *
     * @param wsdLeaveRecords 请假审批记录
     * @return 结果
     */
    @Override
    public int insertWsdLeaveRecords(WsdLeaveRecords wsdLeaveRecords) {
        return wsdLeaveRecordsMapper.insertWsdLeaveRecords(wsdLeaveRecords);
    }

    /**
     * 修改请假审批记录
     *
     * @param wsdLeaveRecords 请假审批记录
     * @return 结果
     */
    @Override
    public int updateWsdLeaveRecords(WsdLeaveRecords wsdLeaveRecords) {
        return wsdLeaveRecordsMapper.updateWsdLeaveRecords(wsdLeaveRecords);
    }

    /**
     * 批量删除请假审批记录
     *
     * @param ids 需要删除的请假审批记录主键
     * @return 结果
     */
    @Override
    public int deleteWsdLeaveRecordsByIds(Long[] ids) {
        return wsdLeaveRecordsMapper.deleteWsdLeaveRecordsByIds(ids);
    }

    /**
     * 删除请假审批记录信息
     *
     * @param id 请假审批记录主键
     * @return 结果
     */
    @Override
    public int deleteWsdLeaveRecordsById(Long id) {
        return wsdLeaveRecordsMapper.deleteWsdLeaveRecordsById(id);
    }

    @Autowired
    private IWsdMealRefundsService wsdMealRefundsService;

    /**
     * 审批请假请求
     * @param wsdLeaveRecords
     * @return
     */
    @Override
    @Transactional
    public int appoveLeave(WsdLeaveRecords wsdLeaveRecords) {
        WsdLeaves wsdLeaves = wsdLeavesMapper.selectWsdLeavesById(wsdLeaveRecords.getLeaveId());
        if (wsdLeaves == null){
            throw new RuntimeException("未找到对应的请假记录");
        }
        LambdaQueryWrapper<WsdLeaveRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WsdLeaveRecords::getLeaveId,wsdLeaveRecords.getLeaveId());
        WsdLeaveRecords wsdLeaveRecords1 = wsdLeaveRecordsMapper.selectList(wrapper).stream().findFirst().orElse(null);
        if (wsdLeaveRecords1 == null){
            throw new RuntimeException("未找到对应的提交请假审批记录");
        }
        wsdLeaves.setStatus(wsdLeaveRecords.getSchoolApprovalStatus());
        wsdLeavesMapper.updateWsdLeaves(wsdLeaves);
        wsdLeaveRecords.setSchoolId(wsdLeaveRecords.getSchoolId());
        wsdLeaveRecords.setSchoolApprovalTime(new Date());
        wsdLeaveRecords.setSchoolApprovalStatus(wsdLeaveRecords.getSchoolApprovalStatus());
        if (wsdLeaveRecords.getSchoolApprovalStatus() == 1){
        //修改
        LambdaUpdateWrapper<WsdMealRefunds> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(WsdMealRefunds::getLeaveId,wsdLeaveRecords.getLeaveId());
        wrapper1.set(WsdMealRefunds::getStatus,2);
        wsdMealRefundsService.update(wrapper1);
        }else {
            //改为不同意退款
            LambdaUpdateWrapper<WsdMealRefunds> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.eq(WsdMealRefunds::getLeaveId,wsdLeaveRecords.getLeaveId());
            wrapper1.set(WsdMealRefunds::getStatus,-1);
            wsdMealRefundsService.update(wrapper1);
        }
        return wsdLeaveRecordsMapper.updateWsdLeaveRecords(wsdLeaveRecords);
    }

    @Override
    public int updateWsdMealRefunds(PayoutsDTO payoutsDTO) {
        WsdMealRefunds wsdMealRefunds = new WsdMealRefunds();
        List<WsdMealRefundsDTO> list = wsdMealRefundsService.selectWsdMealRefundsListById(payoutsDTO.getLeaveId());
        if (list.size()>0){
            BigDecimal refundAmount = BigDecimal.ZERO;
            for (WsdMealRefundsDTO wsdMealRefundsDTO : list){
                wsdMealRefunds.setTransactionId( wsdMealRefundsDTO.getTransactionId() );
                refundAmount = refundAmount.add(wsdMealRefundsDTO.getRefundAmount());
            }
            wsdMealRefunds.setRefundAmount(refundAmount);
        }else {
            throw new RuntimeException("未找到对应的退款记录");
        }
        if (payoutsDTO.getPayoutStatus() == 1){//同意退款
            WsdPaymentRecords wsdPaymentRecords = wsdPaymentRecordsMapper.byTransactionId(wsdMealRefunds.getTransactionId());
//            WsdMealRefunds wsdMealRefunds1 = wsdMealRefundsMapper.selectWsdMealRefundsByMealRefundsId(wsdMealRefunds.getMealRefundsId());
            RefundDTO refundDTO = new RefundDTO();
            refundDTO.setMchntOrderNo(wsdMealRefunds.getTransactionId());
            //订单总金额
            refundDTO.setTotalAmt( wsdPaymentRecords.getTotalFee());
            refundDTO.setRefundAmt( wsdMealRefunds.getRefundAmount()  );
            refundDTO.setSchoolsId( wsdPaymentRecords.getSchoolsId() );
            refundDTO.setStudentId( wsdPaymentRecords.getStudentId() );
            refundDTO.setUserId( wsdPaymentRecords.getUserId() );
            refundDTO.setBalanceFee( wsdPaymentRecords.getBalanceFee() );
            //调用退款接口
            String result = fuiouNotifyService.refund(refundDTO);

            if ("退款成功".equals( result )){
                WsdLeaves wsdLeaves = new WsdLeaves();
                wsdLeaves.setId( payoutsDTO.getLeaveId() );
                wsdLeaves.setPayoutStatus( payoutsDTO.getPayoutStatus() );//退款状态：0=不同意退款, 1=同意退款
                if (payoutsDTO.getPayoutRemarks()!=null&&!"".equals( payoutsDTO.getPayoutRemarks() )){
                    wsdLeaves.setPayoutRemarks( payoutsDTO.getPayoutRemarks() );
                }
                wsdLeavesService.updateWsdLeaves(wsdLeaves);
                //修改餐费退款表状态
                for (WsdMealRefundsDTO wsdMealRefundsDTO : list){
                    WsdMealRefunds wsdMealRefunds2 = new WsdMealRefunds();
                    wsdMealRefunds2.setMealRefundsId( wsdMealRefundsDTO.getMealRefundsId() );
                    wsdMealRefunds2.setStatus( 1 );//退款状态：0=不同意退款, 1=同意退款
                    wsdMealRefundsMapper.updateWsdMealRefunds(wsdMealRefunds2);
                }
                WsdPaymentRecords wsdPaymentRecords1 = new WsdPaymentRecords();
                wsdPaymentRecords1.setPaymentId( wsdPaymentRecords.getPaymentId() );
                wsdPaymentRecords1.setBalanceFee( wsdPaymentRecords.getBalanceFee().add( wsdMealRefunds.getRefundAmount()) );
                return 1;
            }else {
                throw new RuntimeException(result);
            }
        }else {//不同意退款
            //修改请假记录状态
            WsdLeaves wsdLeaves = new WsdLeaves();
            wsdLeaves.setId( payoutsDTO.getLeaveId() );
            wsdLeaves.setPayoutStatus( payoutsDTO.getPayoutStatus() );//退款状态：0=不同意退款, 1=同意退款
            if (payoutsDTO.getPayoutRemarks()!=null&&!"".equals( payoutsDTO.getPayoutRemarks() )){
                wsdLeaves.setPayoutRemarks( payoutsDTO.getPayoutRemarks() );
            }
            wsdLeavesService.updateWsdLeaves(wsdLeaves);
            //修改餐费退款表状态
            for (WsdMealRefundsDTO wsdMealRefundsDTO : list){
                WsdMealRefunds wsdMealRefunds2 = new WsdMealRefunds();
                wsdMealRefunds2.setMealRefundsId( wsdMealRefundsDTO.getMealRefundsId() );
                wsdMealRefunds2.setStatus( 0 );
                wsdMealRefundsMapper.updateWsdMealRefunds(wsdMealRefunds2);
            }
            return 1;
        }

    }
}
